package se.hrbustrc.hlgshopping.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import se.hrbustrc.hlgshopping.entity.Cart;
import se.hrbustrc.hlgshopping.service.CartService;
import se.hrbustrc.hlgshopping.utils.Result;
import se.hrbustrc.hlgshopping.utils.UserContext;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 加入购物车
     */
    @PostMapping
    public Result insertCartAPI(@RequestBody CartRequest request) {
        System.out.println("skuId: " + request.getSkuId());
        System.out.println("count: " + request.getCount());
        System.out.println("userId: " + UserContext.getUserId());
        Result result = new Result();
        try {
            // 从UserContext中获取当前用户ID
            Long userId = UserContext.getUserId();
            if (userId == null) {
                return result.error("用户未登录");
            }
            
            // 将用户ID传递给服务层
            if (cartService.addCart(request.getSkuId(), request.getCount(), userId)) {
                return result.ok("添加成功");
            } else {
                return result.error("添加失败");
            }
        } catch (Exception e) {
            return result.error("添加购物车失败: " + e.getMessage());
        }
    }

    /**
     * 获取购物车列表
     */
    @GetMapping
    public Result getNewCartListAPI() {
        Result result = new Result();
        try {
            // 从UserContext中获取当前用户ID
            Long userId = UserContext.getUserId();
            if (userId == null) {
                return result.error("用户未登录");
            }
            
            // 使用当前用户ID获取购物车列表
            Result cartList = cartService.cartList(userId);
            return result.ok(cartList);
        } catch (Exception e) {
            return result.error("获取购物车列表失败: " + e.getMessage());
        }
    }

    /**
     * 删除购物车
     */
    @DeleteMapping
    public Result delCartAPI(@RequestBody CartDeleteRequest request) {
        System.out.println(request);
        Result result = new Result();
        try {
            // 从UserContext中获取当前用户ID
            Long userId = UserContext.getUserId();
            if (userId == null) {
                return result.error("用户未登录");
            }

            // 删除当前用户的购物车项
            boolean success = cartService.deleteCartItems(request.getIds(), userId);
            if (success) {
                return result.ok("删除成功");
            } else {
                return result.error("删除失败");
            }
        } catch (Exception e) {
            return result.error("删除购物车失败: " + e.getMessage());
        }
    }

    /**
     * 合并购物车
     */
    @PostMapping("/merge")
    public Result mergeCartAPI(@RequestBody List<CartMergeRequest> cartItems) {
        Result result = new Result();
        try {
            // 从UserContext中获取当前用户ID
            Long userId = UserContext.getUserId();
            if (userId == null) {
                return result.error("用户未登录");
            }
            
            // 合并购物车
            boolean success = cartService.mergeCartFromFrontend(cartItems, userId);
            if (success) {
                return result.ok("合并成功");
            } else {
                return result.error("合并失败");
            }
        } catch (Exception e) {
            return result.error("合并购物车失败: " + e.getMessage());
        }
    }
    @PutMapping
    public Result updateCartCountAPI(@RequestBody UpdateCartRequest request) {
        Result result = new Result();
        try {
            // 从UserContext中获取当前用户ID
            Long userId = UserContext.getUserId();
            if (userId == null) {
                return result.error("用户未登录");
            }

            // 更新购物车商品数量
            boolean success = cartService.updateCartItemCount(request.getSkuId(), request.getCount(), userId);
            if (success) {
                return result.ok("更新成功");
            } else {
                return result.error("更新失败，商品不存在于购物车中");
            }
        } catch (Exception e) {
            return result.error("更新购物车商品数量失败: " + e.getMessage());
        }
    }
    
    // 用于接收添加购物车请求的内部类
    public static class CartRequest {
        private Long skuId;
        private Integer count;
        
        // getter和setter方法
        public Long getSkuId() {
            return skuId;
        }
        
        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }
        
        public Integer getCount() {
            return count;
        }
        
        public void setCount(Integer count) {
            this.count = count;
        }
    }
    public static class UpdateCartRequest {
        private Integer count;
        private Long skuId;

        public Long getSkuId() {
            return skuId;
        }
        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }
        // getter和setter方法
        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }
    
    // 用于接收前端合并购物车请求的内部类
    public static class CartMergeRequest {
        private Long id;
        private String name;
        private String picture;
        private String price;
        private Integer count;
        private Long skuId;
        private Boolean selected;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPicture() {
            return picture;
        }

        public void setPicture(String picture) {
            this.picture = picture;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        public Long getSkuId() {
            return skuId;
        }

        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }

        public Boolean getSelected() {
            return selected;
        }

        public void setSelected(Boolean selected) {
            this.selected = selected;
        }
    }
    // 用于接收删除购物车请求的内部类
    public static class CartDeleteRequest {
        private List<Long> ids;

        public List<Long> getIds() {
            return ids;
        }

        public void setIds(List<Long> ids) {
            this.ids = ids;
        }
    }
}
